namespace ManagedFreeFrame
{
    using System;
    using System.Runtime.InteropServices;

    /// <summary>
    ///   contains unsafe methods , using invokers
    /// </summary>
    internal static class UnsafeMethods
    {
        /// <summary>
        /// Loads the library.
        /// Maps the specified DLL file into the address space of the calling process.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <returns></returns>
        [DllImport("kernel32", SetLastError = true)]
        internal static extern IntPtr LoadLibrary(string fileName);

        /// <summary>
        ///  Retrieves the address of an exported function or variable from the specified dynamic-link library (DLL).
        /// </summary>
        /// <param name="pointerToModule">The pointer to module.</param>
        /// <param name="procedureName">Name of the procedure.</param>
        /// <returns></returns>
        [DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
        internal static extern IntPtr GetProcAddress(IntPtr pointerToModule, string procedureName);

        /// <summary>
        /// Frees the loaded dynamic-link library (DLL) module and, if necessary, decrements its reference count. 
        /// When the reference count reaches zero, the module is unloaded from the address space of the calling process and the handle is no longer valid.
        /// </summary>
        /// <param name="module">The module.</param>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        internal static extern int FreeLibrary(int module);

        /// <summary>
        /// Controls whether the system will handle the specified types of serious errors or whether the process will handle them.
        /// </summary>
        /// <param name="modes">The modes.</param>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        internal static extern ErrorModes SetErrorMode(ErrorModes modes);

        [Flags]
        internal enum ErrorModes : uint
        {
            /// <summary>
            /// Use the system default, which is to display all error dialog boxes.
            /// </summary>
            SystemDefault = 0x0,

            /// <summary>
            /// The system does not display the critical-error-handler message box. Instead, the system sends the error to the calling process.
            /// </summary>
            SemFailcriticalerrors = 0x0001,

            /// <summary>
            /// The system automatically fixes memory alignment faults and makes them invisible to the application. It does this for the calling process and any descendant processes. 
            /// </summary>
            SemNoalignmentfaultexcept = 0x0004,

            /// <summary>
            /// The system does not display the Windows Error Reporting dialog.
            /// </summary>
            SemNogpfaulterrorbox = 0x0002,

            /// <summary>
            /// The system does not display a message box when it fails to find a file. Instead, the error is returned to the calling process.
            /// </summary>
            SemNoopenfileerrorbox = 0x8000
        }
    }
}